/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.pages;

import com.miracleteam.miracle.entities.Mathangcuagianhang;
import com.miracleteam.miracle.utils.DSSanPhamMua;
import com.miracleteam.miracle.utils.ThongTinSPMua;
import java.util.List;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;

/**
 *
 * @author DangKhoa
 */
public class MainGioHang {
    //<editor-fold defaultstate="collapsed" desc="Cac thuoc tinh">
    @SessionState(create=false) 
    private DSSanPhamMua DSSanPham;
    
    @Property
    private ThongTinSPMua CTSPM;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Su kien">
    @OnEvent(component="LinkXoaToanBo")
    public Object onXoaHet()
    {
        DSSanPham=null;
        return this;
    }
    @OnEvent(component="LinkXoaHet2")
    public Object onXoaHet2()
    {
        DSSanPham=null;
        return this;
    }
    
    @OnEvent(component="XoaItem")
    public Object onXoaIT(String ma)
    {
        if(DSSanPham==null)
            return this;
        List<ThongTinSPMua> ds=DSSanPham.getDsSanPham();
        for(int i=0;i<ds.size();i++)
        {
            ThongTinSPMua item=(ThongTinSPMua)ds.get(i);
            if(item.getMatHang().getMaMatHangCuaGianHang().compareTo(ma)==0)
            {
                ds.remove(i);
                break;
            }
        }
        DSSanPham.setDsSanPham(ds);
        return this;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Lay thong tin Hien thi">
    public List getDSMua()
    {
        if(DSSanPham!=null){
            return DSSanPham.getDsSanPham();
        }
        return null;
    }
    public float getTongTien()
    {
        return CTSPM.getSoLuong()*CTSPM.getMatHang().getGiaChuaVat();
    }
      public float getThanhTien()
    {
        float kq=0;
        if(DSSanPham==null)
            return 0;
        List<ThongTinSPMua> ds=DSSanPham.getDsSanPham();
        for(int i=0;i<ds.size();i++)
        {
            ThongTinSPMua item=(ThongTinSPMua)ds.get(i);
            kq+=item.getSoLuong()*item.getMatHang().getGiaChuaVat();
        }
        return kq;
    }
    
    //</editor-fold>
}
